﻿using System;
using System.Reflection;
using MessagePack.Formatters;
using MessagePack.Internal;

namespace MessagePack.Resolvers
{
	// Token: 0x02000107 RID: 263
	public class DynamicEnumAsStringResolver : IFormatterResolver
	{
		// Token: 0x060003BB RID: 955 RVA: 0x0000DD71 File Offset: 0x0000C171
		private DynamicEnumAsStringResolver()
		{
		}

		// Token: 0x060003BC RID: 956 RVA: 0x0000DD79 File Offset: 0x0000C179
		public IMessagePackFormatter<T> GetFormatter<T>()
		{
			return DynamicEnumAsStringResolver.FormatterCache<T>.formatter;
		}

		// Token: 0x0400015D RID: 349
		public static readonly IFormatterResolver Instance = new DynamicEnumAsStringResolver();

		// Token: 0x02000108 RID: 264
		private static class FormatterCache<T>
		{
			// Token: 0x060003BE RID: 958 RVA: 0x0000DD8C File Offset: 0x0000C18C
			static FormatterCache()
			{
				TypeInfo_wzh typeInfo = typeof(T).GetTypeInfo_wzh();
				if (typeInfo.IsNullable())
				{
					typeInfo = typeInfo.GenericTypeArguments[0].GetTypeInfo_wzh();
					if (!typeInfo.IsEnum)
					{
						return;
					}
					object formatterDynamic = DynamicEnumAsStringResolver.Instance.GetFormatterDynamic(typeInfo.AsType());
					if (formatterDynamic == null)
					{
						return;
					}
					DynamicEnumAsStringResolver.FormatterCache<T>.formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(new Type[]
					{
						typeInfo.AsType()
					}), new object[]
					{
						formatterDynamic
					});
					return;
				}
				else
				{
					if (!typeInfo.IsEnum)
					{
						return;
					}
					DynamicEnumAsStringResolver.FormatterCache<T>.formatter = (IMessagePackFormatter<T>)new EnumAsStringFormatter<T>();
					return;
				}
			}

			// Token: 0x0400015E RID: 350
			public static readonly IMessagePackFormatter<T> formatter;
		}
	}
}
